package cn.gson.fengboot.common;

import cn.gson.fengboot.controller.DcareController;
import cn.gson.fengboot.model.pojos.Dcare;
import cn.gson.fengboot.model.pojos.Dfercare;
import cn.gson.fengboot.model.pojos.Dsetcare;
import cn.gson.fengboot.model.pojos.TfylinkmanForm;
import cn.gson.fengboot.model.service.DcareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
public class Scheduler{
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");


    @Autowired
    DcareService service;

    @Autowired
    DcareController controller;


    //每天一分钟执行一次执行
    @Scheduled(cron = "0 0/1 * * * ?")
    public void testTasks() {
        //toset();
        System.out.println("定时任务执行时间：" + dateFormat.format(new Date()));
        //toferset();
    }


    public void toset(){
        Dsetcare selectsetce = service.selectsetce();
        String setcaTime = selectsetce.getSetcaTime();
        String[] hour = setcaTime.split(":");

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        Integer a =0;
        for(int i=0;i<hour.length;i++){
            if(Integer.parseInt(hour[i])==(calendar.get(Calendar.HOUR_OF_DAY))){
                a+=1;
            };
            if(Integer.parseInt(hour[i])==(calendar.get(Calendar.MINUTE))){
                a+=1;
            }
           }
        if(a==2){
            List<TfylinkmanForm> selectneed = service.selectneed();
            for (int i=0;i<selectneed.size();i++){
                String linkmanSex="";
                if(selectneed.get(i).getLinkmanSex().equals("女")){
                    linkmanSex="女士";
                }else {
                    linkmanSex="先生";
                }
                String setcaContext = service.selectsetce().getSetcaContext();
                setcaContext=setcaContext.replace("@var(name)",selectneed.get(i).getLinkmanName());
                setcaContext=setcaContext.replace("@var(sex)",selectneed.get(i).getLinkmanSex());

                controller.lastste(selectneed.get(i).getLinkmanMobile(),selectneed.get(i).getLinkmanName(),linkmanSex);
                Dcare dcare = new Dcare(selectneed.get(i),setcaContext,new Timestamp(new  Date().getTime()));
                service.save(dcare);
            }
            a+=3;
        }
    }

    /*判断今天的前一天是否时节假日*/
    public void gettoday(){
        Date date = new Date();
        SimpleDateFormat sdf =new SimpleDateFormat("yyyyMMdd");
        String time=sdf.format(date);
        String dateFlag = getHoliday(time);

        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,-1);
        Date d=cal.getTime();

        String ZUOTIAN=sdf.format(d);//获取昨天日期

        String dateFlag2 = getHoliday(ZUOTIAN);

        /*判断昨天是否时节假日*/
        if ("1".equals(dateFlag2) || "2".equals(dateFlag2)) {
            System.err.println("今天是法定节假日");
        }else {
            if ("1".equals(dateFlag) || "2".equals(dateFlag)) {
                toferset();
                System.err.println("今天是法定节假日");
            }
        }
    }

    /*调用发送节日方法*/
    public void toferset(){
        Dsetcare selectfestivalsetce = service.selectfestivalsetce();
        String setcaTime = selectfestivalsetce.getSetcaTime();
        String[] hour = setcaTime.split(":");

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        Integer a =0;
        for(int i=0;i<hour.length;i++){
            if(Integer.parseInt(hour[i])==(calendar.get(Calendar.HOUR_OF_DAY))){
                a+=1;
            };
            if(Integer.parseInt(hour[i])==(calendar.get(Calendar.MINUTE))){
                a+=1;
            }
        }
        if(a==2){
            List<TfylinkmanForm> selectneed = service.selectallneed();
            for (int i=0;i<selectneed.size();i++){
                String linkmanSex="";
                if(selectneed.get(i).getLinkmanSex().equals("女")){
                    linkmanSex="女士";
                }else {
                    linkmanSex="先生";
                }
                String setcaContext = service.selectsetce().getSetcaContext();
                setcaContext=setcaContext.replace("@var(name)",selectneed.get(i).getLinkmanName());
                setcaContext=setcaContext.replace("@var(sex)",selectneed.get(i).getLinkmanSex());

                controller.lastferste(selectneed.get(i).getLinkmanMobile(),selectneed.get(i).getLinkmanName(),linkmanSex);
                Dfercare dcare = new Dfercare(selectneed.get(i),setcaContext,new Timestamp(new  Date().getTime()));
                service.savefer(dcare);
            }
            a+=3;
        }
    }

    /*判断是否为节假日*/
    public  String  getHoliday(String time) {
        String dc = "http://tool.bitefu.net/jiari/?d=";
        String  httpUrl=new StringBuffer().append(dc).append(time).toString();
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {

        }
        return result;
    }



}
